# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=scite
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-defaults")
pkgver=5.4.2
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.scintilla.org/SciTE.html'
license=('custom:scite')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://www.scintilla.org/${_realname}${pkgver//./}.tgz"
        "0001-Use-POSIX-tools-in-makefiles.patch"
        "0002-Prefix-library-names.patch"
        "0003-Use-FHS.patch")
sha256sums=('b5137ae57a46e27cf2332c60f10cf56b4f736a6e18b191a87bd73131d4ffc2c8'
            '1a8d2679251f53534f4e3a45006c18e44e638e9db24ac870cbbb674149e0e018'
            '161fae196452ca27bb5d28313b3bec12b582af1ac6ee538c03f6229f8a00c45c'
            '8add5e0d508ed16a891060cc2253d2cf25745c9c67f4ded70d50997749c9e161')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"
  apply_patch_with_msg \
    0001-Use-POSIX-tools-in-makefiles.patch \
    0002-Prefix-library-names.patch \
    0003-Use-FHS.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}"/{scite,scintilla,lexilla} "${srcdir}"/build-${MSYSTEM}/
  cd "${srcdir}"/build-${MSYSTEM}

  windir=1 make -C lexilla/src CC=${CC} CXX=${CXX}
  windir=1 GTK3=1 make -C scintilla/gtk CC=${CC} CXX=${CXX}
  windir=1 make -C scintilla/win32 CC=${CC} CXX=${CXX}
  # gnomeprefix=${MINGW_PREFIX} GTK3=1 make -C scite/gtk
  windir=1 make -C scite/win32 CC=${CC} CXX=${CXX}
}

package_scite() {
  pkgdesc="Editor with facilities for building and running programs (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2"
           "${MINGW_PACKAGE_PREFIX}-gtk3")
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}-defaults: Default language files")
  provides=("${MINGW_PACKAGE_PREFIX}-scintilla-gtk3")
  conflicts=("${MINGW_PACKAGE_PREFIX}-scintilla-gtk3")
  replaces=("${MINGW_PACKAGE_PREFIX}-scintilla-gtk3")

  cd "${srcdir}"/build-${MSYSTEM}

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib}

  cp lexilla/bin/liblexilla.dll "${pkgdir}"${MINGW_PREFIX}/bin
  cp lexilla/bin/liblexilla.dll.a "${pkgdir}"${MINGW_PREFIX}/lib

  cp scintilla/bin/libscintillaGtk.dll "${pkgdir}"${MINGW_PREFIX}/bin
  cp scintilla/bin/libscintillaGtk.dll.a "${pkgdir}"${MINGW_PREFIX}/lib

  cp scintilla/bin/libScintilla.dll "${pkgdir}"${MINGW_PREFIX}/bin
  cp scintilla/bin/libScintilla.dll.a "${pkgdir}"${MINGW_PREFIX}/lib

  cp scite/bin/SciTE.exe "${pkgdir}"${MINGW_PREFIX}/bin

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/include/scintilla
  cp scintilla/include/{*.h,*.iface} "${pkgdir}"${MINGW_PREFIX}/include/scintilla

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/scite
  cp scite/bin/SciTEGlobal.properties "${pkgdir}"${MINGW_PREFIX}/share/scite

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/doc/scite
  cp scite/doc/{*.png,*.jpg,*.html} "${pkgdir}"${MINGW_PREFIX}/share/doc/scite

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/man/man1
  cp scite/doc/scite.1 "${pkgdir}"${MINGW_PREFIX}/share/man/man1/SciTE.1

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite
  cp scintilla/License.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-scintilla
  cp scite/License.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-scite
  cp scite/lua/COPYRIGHT "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-lua
}

package_scite-defaults() {
  pkgdesc="Default language files for the SciTE editor (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")

  cd "${srcdir}"/build-${MSYSTEM}

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/scite
  cp scite/bin/*.properties "${pkgdir}"${MINGW_PREFIX}/share/scite
  rm -f "${pkgdir}"${MINGW_PREFIX}/share/scite/SciTEGlobal.properties
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
